import os
import sys
import tty
import termios
from trie import Trie

words = [
    "as", "astronaut", "asteroid", "are", "around",
    "cat", "cars", "cares", "careful", "carefully",
    "for", "follows", "forgot", "from", "front",
    "mellow", "mean", "money", "monday", "monster",
    "place", "plan", "planet", "planets", "plans",
    "the", "their", "they", "there", "towards"
]

dictionary = None

def initialize_trie(words):
    trie = Trie()
    for word in words:
        trie.insert(word)
    return trie

def search_word():
    print_trie(dictionary)
    while True:
        print("Enter a word to search for, or press Enter to exit.")
        input_word = input()
        if input_word == "":
            break
        if input_word is not None and dictionary.search(input_word):
            print(f'Found "{input_word}" in dictionary')
        else:
            print(f'Did not find "{input_word}" in dictionary')

def prefix_autocomplete():
    print_trie(dictionary)
    get_prefix_input()

def delete_word():
    print_trie(dictionary)
    while True:
        print("\nEnter a word to delete, or press Enter to exit.")
        input_word = input()
        if input_word == "":
            break
        if input_word is not None and dictionary.search(input_word):
            dictionary.delete(input_word)
            print(f'Deleted "{input_word}" from dictionary\n')
            print_trie(dictionary)
        else:
            print(f'Did not find "{input_word}" in dictionary')

def get_spelling_suggestions():
    print_trie(dictionary)
    print("\nEnter a word to get spelling suggestions for, or press Enter to exit.")
    input_word = input()
    if input_word is not None:
        similar_words = dictionary.get_spelling_suggestions(input_word)
        print(f'Spelling suggestions for "{input_word}":')
        if len(similar_words) == 0:
            print("No suggestions found.")
        else:
            for word in similar_words:
                print(word)

def run_all_exercises():
    print_trie(dictionary)
    search_word()
    prefix_autocomplete()
    delete_word()
    get_spelling_suggestions()

def get_prefix_input():
    while True:
        print("\nEnter a prefix to search for, or press Enter to exit.")
        prefix = input()
        if prefix == "":
            break
        
        matches = dictionary.auto_suggest(prefix)
        if matches:
            print(f"\nWords starting with '{prefix}':")
            for word in matches:
                print(word)
        else:
            print(f"No words found starting with '{prefix}'")

def print_trie(trie):
    print("The dictionary contains the following words:")
    words = trie.get_all_words()
    for word in words:
        print(f"{word}, ", end='')
    print()

if __name__ == "__main__":
    dictionary = initialize_trie(words)
    search_word()
    prefix_autocomplete()
    delete_word()
    get_spelling_suggestions()